function init(){
	var page = 1;
	var rows = 10;
	
	/**
	 * 订单号查询的方法
	 */
	$('.search-box').on('click','span',function(){
		var orderId = $('#orderId').val();
		var deliveryDate = $('#datePicker').val();
		var beginTime,endTime;
		if(deliveryDate != ''){
			var deliveryDates = deliveryDate.split(' - ');
			beginTime = deliveryDates[0];
			endTime = deliveryDates[1];
		}
		loadComment(page, rows, orderId, beginTime, endTime);
	});
	
	/**
	 * 发货时间查询的方法
	 */
	$('#datePicker').on('change',function(e){
		var orderId = $('#orderId').val();
		var deliveryDate = $(e.target).val();
		var beginTime,endTime;
		if(deliveryDate != ''){
			var deliveryDates = deliveryDate.split(' - ');
			beginTime = deliveryDates[0];
			endTime = deliveryDates[1];
		}
		loadComment(page, rows, orderId, beginTime, endTime);
	});
	
	/**
	 * 导出功能
	 */
	$('.export').on('click','li',function(){
		$.ajax({
			type: "post",
			url: '/diconet/delivery/export',
			dataType: 'json',
			success: function(data){
			},
			error: function(data){
			}
		});
	});
	
	/**
	 * 查看的方法
	 */
	$('.data-tbody').on('click','.detail',function(e){
		var orderId = $(this).attr('data-id');
		findDetail(orderId);
	});
	/**
	 * 审核的方法
	 */
	$('.data-tbody').on('click','.examine',function(e){
		var orderId = $(this).attr('data-id');
		window.location.href = path + "/distributor/order_approval?id="+orderId;
	});
	/**
	 * 出库的方法
	 */
	$('.data-tbody').on('click','.outbound',function(e){
		var orderId = $(this).attr('data-id');
		window.location.href = path + "/distributor/stock_out?id="+orderId;
	});
	
	//w调用日期选择插件
	$('#datePicker').daterangepicker();
	loadComment(page, rows, '', '', '');
}

/**
 * 加载列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, orderId, beginTime, endTime) {
	$.ajax({
		type: "post",
		url: '/diconet/delivery/list/page',
		data: {
			page : page,
			rows : rows,
			orderId : orderId,
			beginTime : beginTime,
			endTime : endTime
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<tr data-id="'+comment.id+'">';
					commentContent += '<td class="t1">'+comment.deliveryCode+'</td>';
					commentContent += '<td class="t1">'+comment.orderCode+'</td>';
					commentContent += '<td class="t1">'+comment.consignee+'</td>';
					commentContent += '<td class="t1">'+comment.mobile+'</td>';
					var approve;
					if(comment.approve == 2){
						approve = '审批驳回';
					}else if(comment.approve == 1){
						approve = '审批通过';
					}else{
						approve = '未审批';
					}
					var status;
					if(comment.status == 2){
						status = '发货完成';
					}else if(comment.status == 1){
						status = '发货中';
					}else{
						status = '未发货';
					}
					commentContent += '<td class="t1">'+approve+'</td>';
					commentContent += '<td class="t1">'+status+'</td>';
					commentContent += '<td><button data-id="'+comment.id+'" class="detail">查看</button>';
					if(comment.approve != 2  && comment.approve != 1){
						commentContent += '<button data-id="'+comment.deliveryId+'" class="examine">审核</button>';
					}
					if(comment.status != 2 && comment.approve == 1){
						commentContent += '<button data-id="'+comment.deliveryId+'" class="outbound">出库</button>';
					}
					commentContent += '</tr>';
				}
				$('.data-tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+orderId+'\',\''+beginTime+'\',\''+endTime+'\');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}

/**
 * 查看订单详情
 * @param id
 * @returns
 */
function findDetail(id){
	window.location.href = path + "/distributor/orderDetail?id="+id;
}

init();